package com.xmy.cultivate.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xmy.cultivate.entity.*;
import com.xmy.cultivate.mapper.EnrollSetMapper;
import com.xmy.cultivate.service.*;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;

/**
 * <p>
 * 招生信息设置 服务实现类
 * </p>
 *
 * @author hpiggy
 * @since 2024-08-26
 */
@Service
public class EnrollSetServiceImpl extends ServiceImpl<EnrollSetMapper, EnrollSet> implements IEnrollSetService {

    @Autowired
    IEnrollCustomerService iEnrollCustomerService;

    @Autowired
    IStudentService studentService;

    @Autowired
    IOrganizationService iorganizationService;

    @Autowired
    IEnrollStageTimeService iEnrollStageTimeService;

    @Autowired
    IQuarterService iQuarterService;


    @Override
    public EnrollSet findEnrollSetForDate(Long schoolId) {
        LocalDate localDateNow = LocalDate.now();
        Organization organization = iorganizationService.getById(schoolId);
        if (organization != null) {
            QueryWrapper<EnrollSet> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("level",organization.getLevel());
            queryWrapper.le("linkage_start_date",localDateNow);
            queryWrapper.ge("linkage_end_date",localDateNow);
            queryWrapper.last("limit 1");
            EnrollSet enrollSet = this.getOne(queryWrapper);
            return enrollSet;
        }else {
            return null;
        }
    }


    @Override
    public EnrollCustomer getPhoneCustomer(Long studentId) {
        Student student = studentService.getById(studentId);
        LocalDate localDate = LocalDate.now();

        Quarter quarterNow = iQuarterService.getNowQuarter();

        QueryWrapper<EnrollStageTime> enrollStageTimeQueryWrapper = new QueryWrapper<>();
        enrollStageTimeQueryWrapper.eq("year_part",quarterNow.getYearPart());
        enrollStageTimeQueryWrapper.eq("quarter_num",quarterNow.getNum());
        enrollStageTimeQueryWrapper.eq("stage_type_id",1833416377023344641L);//日常招生
        enrollStageTimeQueryWrapper.le("start_date",localDate);
        enrollStageTimeQueryWrapper.ge("end_date",localDate);
        enrollStageTimeQueryWrapper.orderByDesc("id");
        enrollStageTimeQueryWrapper.last("limit 1");
        EnrollStageTime enrollStageTime = iEnrollStageTimeService.getOne(enrollStageTimeQueryWrapper);

        QueryWrapper<EnrollCustomer> enrollCustomerQueryWrapper = new QueryWrapper<>();
        enrollCustomerQueryWrapper.orderByAsc("created_at","id");
        enrollCustomerQueryWrapper.eq("phone",student.getLinkmanPhone());
        enrollCustomerQueryWrapper.last("limit 1");

        if (enrollStageTime!=null){
            //日常招生
            //enrollCustomerQueryWrapper.gt("add_date",enrollDate);
            EnrollCustomer enrollCustomer = iEnrollCustomerService.getOne(enrollCustomerQueryWrapper);
            return enrollCustomer;
        }else {
            return null;
        }
    }

}
